﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.Sakila.TestSite.Objects;
using Acme.Sakila.EFDAL;
using nHydrate.EFCore.DataAccess;
using Acme.Sakila.EFDAL.Entity;

namespace Acme.Sakila.TestSite
{
	public partial class CategoryList : BasePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			grdItem.RowDataBound += new GridViewRowEventHandler(grdItem_RowDataBound);
			grdItem.RowCommand += new GridViewCommandEventHandler(grdItem_RowCommand);
			this.PagingControl1.ObjectSingular = "Category";
			this.PagingControl1.ObjectPlural = "Categories";
			this.PagingControl1.RecordsPerPageChanged += new EventHandler(PagingControl1_RecordsPerPageChanged);
			cmdAdd.Click += new EventHandler(cmdAdd_Click);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!this.IsPostBack)
			{
				this.Populate();
			}
		}

		#endregion

		#region Methods

		private void Populate()
		{
			using (var context = new SakilaEntities(Utils.ContextStartup))
			{
				var paging = new Paging(
					this.PagingControl1.PageIndex,
					this.PagingControl1.RecordsPerPage);

				var categoryCollection = context.category.GetPagedResults(x => x.category_id, paging);

				this.PagingControl1.ItemCount = paging.RecordCount;
				grdItem.DataSource = categoryCollection;
				grdItem.DataBind();
				SessionHelper.LastCategoryListSearch = this.Request.Url.AbsoluteUri;
			}
		}

		#endregion

		#region Event Handlers

		private void grdItem_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var theCategory = e.Row.DataItem as Acme.Sakila.EFDAL.Entity.category;
				var linkEdit = (HyperLink)e.Row.FindControl("linkEdit");
				var linkDelete = (LinkButton)e.Row.FindControl("linkDelete");
				var linkFilms = (HyperLink)e.Row.FindControl("linkFilms");

				linkEdit.NavigateUrl = "/CategoryItem.aspx?id=" + theCategory.category_id;

				linkDelete.Attributes.Add("onclick", "return confirm('Do you wish to delete this item?');");
				linkDelete.CommandArgument = theCategory.category_id.ToString();
				linkDelete.CommandName = "GoDelete";

				linkFilms.NavigateUrl = "/FilmList.aspx?categoryid=" + theCategory.category_id;
			}
		}

		private void grdItem_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "GoDelete")
			{
				int id = int.Parse((string)e.CommandArgument);
				try
				{
					using (var context = new SakilaEntities(Utils.ContextStartup))
					{
						var item = context.category.FirstOrDefault(x => x.category_id == id);
						context.DeleteItem(item);
						context.SaveChanges();
					}
				}
				catch (Exception ex)
				{
					lblError.Text = "The item has dependencies and cannot be deleted.";
					return;
				}
				this.Populate();
			}
		}

		private void PagingControl1_RecordsPerPageChanged(object sender, EventArgs e)
		{
			var query = new URL(this.Request.Url.AbsoluteUri);
			query.Parameters.SetValue("rpp", PagingControl1.RecordsPerPage.ToString());
			query.Parameters.SetValue("po", "1");
			this.Response.Redirect(query.ToString());
		}

		private void cmdAdd_Click(object sender, EventArgs e)
		{
			this.Response.Redirect("/CategoryItem.aspx");
		}

		#endregion

	}
}